<?php
/**
 * Handler File Class
 *
 * @author admin <admin@wolive.cc>
 * @email admin@wolive.cc
 * @date 2017/06/01
 */

namespace app\mobile\controller;

use think\Controller;
use app\mobile\model\User;
use app\Common;


/**
 *
 * 前台手机端控制器.
 * Class Index
 * @package app\mobile\controller
 */
class Index extends Controller
{
    /**
     * 唯一随机数方法
     * [rand description]
     * @param  [type] $len [description]
     * @return [type]      [description]
     */
    public function rand($len)
    {
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $string = substr(time(), -3);
        for (; $len >= 1; $len--) {
            $position = rand() % strlen($chars);
            $position2 = rand() % strlen($string);
            $string = substr_replace($string, substr($chars, $position, 1), $position2, 0);
        }
        return $string;
    }


    /**
     *
     * [home description]
     * @return [type] [description]
     */
    public function home()
    {
        $data = $this->request->request();

        $data['product'] = empty($data['product']) ? '' : $data['product'];

        // 传递商品信息
        if ($data['product']) {
            $product = json_decode(htmlspecialchars_decode($data['product']), true);
            if (isset($product['pid']) && isset($product['url']) && isset($product['img']) && isset($product['title']) && isset($product['info']) && isset($product['price'])) {

                $arr2['timestamp'] = time();

                $service_id = 0;

                $str = '<a href="' . ($product['url']) . '" target="_blank" class="wolive_product_msg">';
                $str .= '<img src="' . ($product['img']) . '" >';
                $str .= '<div class="product_info"><p class="wolive_product_title">' . ($product['title']) . '</p>';
                $str .= '<p class="wolive_product_info">' . ($product['info']) . '</p>';
                $str .= '<p class="wolive_product_price">' . ($product['price']) . '</p>';
                $str .= '</div></a>';

                $mydata = ['service_id' => ($service_id), 'visiter_id' => ($data['visiter_id']), 'content' => $str, 'timestamp' => time(), 'business_id' => ($data['business_id']), 'direction' => 'to_service'];

                User::table('wolive_chats')->insert($mydata);
            }
        }

        $service_name = isset($data['service_name']) ? $data['service_name'] :'';

        $str = "visiter_id=" . $data['visiter_id'] . "&visiter_name=" . $data['visiter_name'] . "&avatar=" . $data['avatar'] . "&business_id=" . $data['business_id'] . "&service_name=$service_name" . "&groupid=" . $data['groupid'];

        $common = new Common();

        $newstr = $common->encrypt($str, 'E', 'wolive');

        $a = urlencode($newstr);

        $this->redirect('/mobile/index?code=' . $a);

    }


    /**
     *
     * 手机端首页.
     *
     * @return mixed
     */
    public function index()
    {

        $url = domain;
        $arr = $this->request->get();

        $common = new Common();

        $data = $common->encrypt($arr['code'], 'D', 'wolive');

        if (!$data) {
            $this->redirect('index/index/errors');
        }

        parse_str($data, $arr2);

        if (!isset($arr2['visiter_id']) || !isset($arr2['visiter_name']) || !isset($arr2['groupid']) || !isset($arr2['business_id']) || !isset($arr2['avatar'])) {
            $this->redirect('index/index/errors');
        }

        $exclusive_service_name = isset($arr2['service_name']) ? $arr2['service_name'] : '';
        $business_id = ($arr2['business_id']);
        $visiter_id = ($arr2['visiter_id']);
        if ($visiter_id === '') {
            if (empty($_SESSION['Visiter']['visiter_id'])) {
                $visiter_id = str_replace(['+','/', '='] ,'' , base64_encode(pack('Nn', time(), rand(1, 65535))));
                $_SESSION['Visiter']['visiter_id'] = $visiter_id;
            } else {
                $visiter_id = $_SESSION['Visiter']['visiter_id'];
            }
        }

        $channel = bin2hex($visiter_id . '/' . $business_id);
        $visiter_name = ($arr2['visiter_name']);
        if (isset($_SERVER['HTTP_REFERER'])) {
            $from_url = $_SERVER['HTTP_REFERER'];
        } else {
            $from_url = '';
        }
        $avatar = ($arr2['avatar']);
        $groupid = ($arr2['groupid']);

        if ($visiter_name == '') {
            $visiter_name = '游客' . $visiter_id;
        }
        $app_key = app_key;
        $whost = whost;
        $arr = parse_url($whost);
        if ($arr['scheme'] == 'ws') {
            $port = 'wsPort';
            $value = 'false';
        } else {
            $value = 'true';
            $port = 'wssPort';
        }

        $business = User::table('wolive_business')->where('business_id', $business_id)->find();

        $in_black_list = 0;
        $res = User::table("wolive_queue")->where(['visiter_id' => $visiter_id, 'business_id' => $business_id])->find();
        if ($res && $res['state'] == 'in_black_list') {
            $in_black_list = 1;//是黑名单
        }
        $group = User::table('wolive_group')->where('id', $groupid)->find();
        $this->assign("group",$group);

        $this->assign("in_black_list", $in_black_list);
        $this->assign("atype", $business['audio_state']);
        $this->assign('groupid', $groupid);
        $this->assign('app_key', $app_key);
        $this->assign('whost', $arr['host']);
        $this->assign('value', $value);
        $this->assign('wport', wport);
        $this->assign('port', $port);
        $this->assign('url', $url);
        $this->assign('visiter', $visiter_name);
        $this->assign('business_id', $business_id);
        $this->assign('exclusive_service_name', $exclusive_service_name);
        $this->assign('from_url', $from_url);
        $this->assign('channel', $channel);
        $this->assign('visiter_id', $visiter_id);
        $this->assign('avatar', $avatar);
        return $this->fetch();
    }

}
